package dsk.demo.product.vo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import dsk.demo.common.utils.StreamUtils;
import dsk.demo.product.entity.ProductCarEntity;
import dsk.demo.product.entity.ProductEntity;
import dsk.demo.product.entity.ProductRefEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductVo extends ProductEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    private String catalogName;
    private String catalogEnname;
    private String customerNo;
    private List<ProductRefEntity> oemList;
    private List<ProductCarEntity> carList;
    private Integer order;

    @Override
    public boolean equals(Object o){
        if (!(o instanceof ProductVo)) {
            return false;
        }
        ProductVo vo = (ProductVo) o;
        if (StrUtil.isEmpty(vo.getProductNo()) || StrUtil.isEmpty(this.getProductNo())) {
            return false;
        }
        return this.getProductNo().equals(vo.getProductNo());
    }

    public void dealReturnOemList() {
        Map<String, List<ProductRefEntity>> brandMap = StreamUtils.groupByKey(this.getOemList(), ProductRefEntity::getBrand);
        Set<String> strings = StreamUtils.toSet(this.getOemList(), ProductRefEntity::getBrand);
        if (brandMap.size() > 0) {

            ArrayList<String> brandList = new ArrayList<>(strings);
            ArrayList<ProductRefEntity> finalOemList = new ArrayList<>();

            for (int i=0;i< Math.min(this.getOemList().size(), 6); i++) {
                List<ProductRefEntity> productRefEntities = brandMap.get(brandList.get(i % brandList.size()));
                finalOemList.add(productRefEntities.get(0));
                productRefEntities.remove(0);
                if (CollectionUtil.isEmpty(productRefEntities)) {
                    brandMap.remove(brandList.get(i % brandList.size()));
                    brandList.remove(i % brandList.size());
                }
            }
            this.setOemList(finalOemList);
        }
    }
}
